package com.company.cloud.user.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.HttpContextUtils;
import com.company.cloud.common.utils.PasswordUtils;
import com.company.cloud.user.modules.app.controller.req.UserLoginByMobile2CodeParam;
import com.company.cloud.user.modules.base.entity.ThirdUserInfo;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.mapper.UserInfoMapper;
import com.company.cloud.user.modules.base.service.ThirdUserInfoService;
import com.company.cloud.user.modules.base.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.cloud.user.modules.cache.models.ArtistInfoModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 1_用户信息表  服务实现类
 * </p>
 *
 * @author anros
 * @since 2020-06-11
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private ThirdUserInfoService thirdUserInfoService;

    @Override
    public void modifyPassword(UserInfo userInfo, UserLoginByMobile2CodeParam params) {
        String[] passwords = PasswordUtils.getMd5Password(params.getPassword());
        userInfo.setSalt(passwords[1]);
        userInfo.setPassword(passwords[0]);
        userInfo.setEdt(LocalDateTime.now());
        userInfo.setEip(HttpContextUtils.getClientIP());
        boolean flag = updateById(userInfo);
        if (!flag) {
            throw new BusinessException("密码修改失败!");
        }
    }

    /**
     * 用户注销
     *
     * @param userInfo
     * @param params
     */
    @Override
    public void modifyCancel(UserInfo userInfo, UserLoginByMobile2CodeParam params) {
        userInfo.setUserState("2");
        userInfo.setEdt(LocalDateTime.now());
        userInfo.setEip(HttpContextUtils.getClientIP());
        boolean flag = updateById(userInfo);
        if (!flag) {
            throw new BusinessException("密码修改失败!");
        }
        /*//清除用户信息列表的user_id 改为 -1
        List<ThirdUserInfo> list = thirdUserInfoService.list(new QueryWrapper<ThirdUserInfo>()
                .eq("user_id", userInfo.getId()));

        for (ThirdUserInfo thirdUserInfo : list) {
            thirdUserInfo.setUserId(-1);
            thirdUserInfo.setId(thirdUserInfo.getId());
            thirdUserInfoService.updateById(thirdUserInfo);
        }*/
    }


}
